package it.eng.area118.mdogpscollector.db;

import java.util.ArrayList;
import java.util.StringTokenizer;

import it.eng.area118.mdogpscollector.model.PerstRootBean;

import org.garret.perst.Index;
import org.garret.perst.Storage;
import org.garret.perst.StorageFactory;

public class DbManager {

	public final static int WRITE_MODE = 1;

	protected Storage db;
	protected PerstRootBean root;

	/* for read!!!! */
	public static Storage getDb(String file) {
		Storage db = StorageFactory.getInstance().createStorage();
		db.open(file, 100 * 1000);
		return db;
		// return (PerstRootBean) db.getRoot();
	}

	public DbManager(Storage db) {
		this.db = db;
		this.root = (PerstRootBean) db.getRoot();
	}

	public DbManager(String file, int writeMode) {
		this(file, 100 * 1000, writeMode);
	}

	public DbManager(String file, int pageSize, int createMode) {
		this.db = StorageFactory.getInstance().createStorage();
		try {
			this.db.open(file, pageSize);
		} catch (Exception e) {
			System.out.println("error");
		}

		root = (PerstRootBean) db.getRoot();
		if (root == null) {
			if (createMode == WRITE_MODE) {
				root = new PerstRootBean(db);
				db.setRoot(root);
			} else {
				System.out.println("error");
			}
		}
	}

	public <E extends Item> void removeIndex(Class<E> clazz, E obj) {
		Index index = root.getIndex(clazz, db, false);
		index.remove(obj.getItemKey(), obj);
		StringTokenizer tonizer = new StringTokenizer(obj.getItemKey(), "-");
		//
		ArrayList list = new ArrayList();
		while (tonizer.hasMoreTokens()) {
			list.add(tonizer.nextToken());
		}
		if (list.size() > 1) {
			// faccio l'aggiunta ulteriore solo se il comune ha un nome composto
			System.out.println("HAS MOR TOKENS: " + list);
			index.remove(list.get(0) + "-" + list.get(1), obj);
			index.remove(list.get(0) + "-" + list.get(2), obj);
			index.remove(list.get(1) + "-" + list.get(2), obj);
			for (int i = 0; i < list.size(); i++) {
				index.remove((String) list.get(i), obj);
			}

		}
		db.commit();
	}

	public <E extends Item> void storeObjectWithIndex(Class<E> clazz, E obj) {
		Index index = root.getIndex(clazz, db, false);
		index.put(obj.getItemKey(), obj);
		StringTokenizer tonizer = new StringTokenizer(obj.getItemKey(), "-");
		//
		ArrayList list = new ArrayList();
		while (tonizer.hasMoreTokens()) {
			list.add(tonizer.nextToken());
		}
		if (list.size() > 1) {
			// faccio l'aggiunta ulteriore solo se il comune ha un nome composto
			System.out.println("HAS MOR TOKENS: " + list);
			index.put(list.get(0) + "-" + list.get(1), obj);
			index.put(list.get(0) + "-" + list.get(2), obj);
			index.put(list.get(1) + "-" + list.get(2), obj);
			for (int i = 0; i < list.size(); i++) {
				index.put((String) list.get(i), obj);
			}

		}
		db.commit();
	}

	public Storage reset(String file) {
		try {
			close();
		} catch (Exception e) {

		}

		this.db = StorageFactory.getInstance().createStorage();
		try {
			this.db.open(file, 100 * 1000);
		} catch (Exception e) {
			System.out.println("error");
		}
		root = (PerstRootBean) db.getRoot();
		this.root = (PerstRootBean) db.getRoot();
		return this.db;
	}

	public void close() {
		db.close();
	}
}
